package com.cl.edu.web.teacher;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.cl.core.orm.Page;
import com.cl.core.web.struts2.Struts2Utils;
import com.cl.edu.entity.Area;
import com.cl.edu.entity.Bookingstudent;
import com.cl.edu.entity.Message;
import com.cl.edu.entity.News;
import com.cl.edu.entity.Subject;
import com.cl.edu.entity.Teacher;
import com.cl.edu.service.RegisterManager;
import com.cl.edu.service.teacher.MessageManager;
import com.cl.edu.service.teacher.TeacherManager;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

/**
 * 教员管理系统
 * @author Administrator
 *
 */

@Results( { @Result(name = "home", location = "tc_apply_home.jsp"),
	@Result(name = "mod", location = "tc_jianli_mod.jsp"),
	@Result(name = "password", location = "tc_password.jsp"),
	@Result(name = "jianli", location = "tc_jianli_state.jsp"),
	@Result(name = "log", location = "tc_apply_log.jsp")})
public class ManageAction extends ActionSupport implements Preparable,ModelDriven<Teacher>{
	
	@Autowired
	private TeacherManager teacherManager;
	@Autowired
	private RegisterManager registerManager;
	@Autowired
	private MessageManager messageManager;
	
	private Teacher entity;
	private Long id;					//教员id
	private Integer  msgId;				//消息id
	
	private List<Subject> subjectList;//教师任教学科列表
	private List<Area>  syList;//沈阳区划
	
	private List<String> myFirstJobList = new ArrayList<String>();//教员已选择的教学科目
	private List<String> secondTeachPlaceList  = new ArrayList<String>();//已选择的可授课区域
	
	private Page<Bookingstudent> page = new Page<Bookingstudent>();
	
	private Page<Message> messages = new Page<Message>();//未读消息列表
	/**
	 * 教员管理系统主页
	 * 查询未读站内消息
	 * @return
	 * @throws Exception
	 */
	public String execute() throws Exception {
		if (!messages.isOrderBySetted()) {
			messages.setOrderBy("createTime");
			messages.setOrder(Page.DESC);
		}
		entity = (Teacher)Struts2Utils.getSession().getAttribute("teacher");
		id = entity.getId();
		messages = messageManager.getNoReadMessages(messages, id);
		return "home";
	}
	
	/**
	 * 预约申请记录
	 * @return
	 * @throws Exception
	 */
	public String log() throws Exception {
		if (!page.isOrderBySetted()) {
			page.setOrderBy("bookTime");
			page.setOrder(Page.DESC);
		}
		page = teacherManager.getBookingstudents(page,id);
		return "log";
	}
	
	
	/**
	 * 修改简历前获取信息
	 * @return
	 * @throws Exception
	 */
	public String mod() throws Exception {
		subjectList   = registerManager.getSubjects("2");
		syList        = registerManager.getShenYang();
		String myFirstJob = entity.getFirstSubject();
		String[] jobs = myFirstJob.split(",");
		for(String job:jobs){
			myFirstJobList.add(job);
		}
		
		String secondTeachPlace = entity.getTeachPlace();
		String[] places = secondTeachPlace.split(",");
		for(String place:places) {
			secondTeachPlaceList.add(place);
		}
		return "mod";
	}
	
	/**
	 * 修改个人信息
	 * @return
	 * @throws Exception
	 */
	public String save() throws Exception{
		try {
			entity.setState(1);//修改信息后设置为待审核
			teacherManager.updateTeacher(entity);
			Struts2Utils.getSession().setAttribute("teacher",entity);
			addActionMessage("您的个人信息修改成功!");
		} catch (Exception e) {
			addActionMessage("您的个人信息修改失败!");
			e.printStackTrace();
		}
		return mod();
	}
	
	/**
	 * 修改密码
	 * @return
	 * @throws Exception
	 */
	public String changePassword() throws Exception {
		entity = teacherManager.getTeacher(id);
		entity.setPassword(Struts2Utils.getParameter("new_password"));
		try {
			teacherManager.updateTeacher(entity);
			Struts2Utils.getSession().setAttribute("teacher",entity);
			addActionMessage("您的密码修改成功!");
		} catch (Exception e) {
			addActionMessage("您的密码修改失败!");
			e.printStackTrace();
		}
		return "password";
	}
	
	/**
	 * 修改简历状态
	 * @return
	 * @throws Exception
	 */
	public String editJianli() throws Exception {
		entity = teacherManager.getTeacher(id);
		entity.setJianliFlag(Integer.valueOf(Struts2Utils.getParameter("jianli_flag")));
		try {
			teacherManager.updateTeacher(entity);
			Struts2Utils.getSession().setAttribute("teacher",entity);
			addActionMessage("您的简历状态修改成功!");
		} catch (Exception e) {
			addActionMessage("您的简历状态修改失败!");
			e.printStackTrace();
		}
		return "jianli";
	}
	
	/**
	 * 更新消息状态
	 * @return
	 * @throws Exception
	 */
	public String set() throws Exception{
		Message message = messageManager.getMessage(msgId);
		message.setIsRead(1);
		messageManager.updateMessage(message);
		addActionMessage("更改状态成功!");
		return execute();
	}
	
	/**
	 * 删除一条消息 
	 * @return
	 * @throws Exception
	 */
	public String del() throws Exception {
		messageManager.delMessage(msgId);
		addActionMessage("删除成功!");
		return execute();
	}


	/**
	 * 
	 * @return
	 */
	public Teacher getModel() {
		return entity;
	}

	/**
	 * 在进入save方法前执行
	 * @throws Exception
	 */
	public void prepareSave() throws Exception {
		prepareModel();
	}
	
	/**
	 * 在进入mod方法时执行
	 * @throws Exception
	 */
	public void prepareMod() throws Exception {
		prepareModel();
	}
	
	private void prepareModel() {
		if(id == null) {
			entity  = new Teacher();
		} else {
			entity = teacherManager.getTeacher(id);
		}
	}

	/**
	 * 不是所有方法都需要 空实现
	 */
	public void prepare() throws Exception {
	}


	public Teacher getEntity() {
		return entity;
	}


	public void setEntity(Teacher entity) {
		this.entity = entity;
	}


	public Long getId() {
		return id;
	}


	public void setId(Long id) {
		this.id = id;
	}


	public List<Subject> getSubjectList() {
		return subjectList;
	}


	public void setSubjectList(List<Subject> subjectList) {
		this.subjectList = subjectList;
	}


	public List<Area> getSyList() {
		return syList;
	}


	public void setSyList(List<Area> syList) {
		this.syList = syList;
	}


	public List<String> getMyFirstJobList() {
		return myFirstJobList;
	}


	public void setMyFirstJobList(List<String> myFirstJobList) {
		this.myFirstJobList = myFirstJobList;
	}


	public List<String> getSecondTeachPlaceList() {
		return secondTeachPlaceList;
	}


	public void setSecondTeachPlaceList(List<String> secondTeachPlaceList) {
		this.secondTeachPlaceList = secondTeachPlaceList;
	}

	public Page<Bookingstudent> getPage() {
		return page;
	}

	public void setPage(Page<Bookingstudent> page) {
		this.page = page;
	}

	public Page<Message> getMessages() {
		return messages;
	}

	public void setMessages(Page<Message> messages) {
		this.messages = messages;
	}

	public Integer getMsgId() {
		return msgId;
	}

	public void setMsgId(Integer msgId) {
		this.msgId = msgId;
	}

	
	
}
